package org.swan.bean;


import javax.websocket.*;
import java.awt.print.PrinterJob;
import java.io.File;
import java.io.IOException;
import java.net.URI;


@ClientEndpoint
public class WebSocketClient {

    private Session userSession = null;

    public WebSocketClient(URI endpointURI) {
        try {
            WebSocketContainer container = ContainerProvider.getWebSocketContainer();
            container.connectToServer(this, endpointURI);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @OnOpen
    public void onOpen(Session userSession) {
        this.userSession = userSession;
        System.out.println("Connected to server");
    }

    @OnClose
    public void onClose(Session userSession, CloseReason reason) {
        this.userSession = null;
        System.out.println("Connection closed: " + reason);
    }

    @OnMessage
    public void onMessage(String message) {
      System.out.println("Received message: " + message);

    }

    public void sendMessage(String message) {
        if (this.userSession != null) {
            this.userSession.getAsyncRemote().sendText(message);
        }
    }
}
